package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleDao roleDao;
    @Autowired
    PermissionDao permissionDao;
    @Autowired
    MenuDao menuDao;

    public List<Role> findAll(){
        return  roleDao.findAll();
    }

    @Override
    public List<Integer> findroleIdsByUserId(Integer userId) {
        return roleDao.findroleIdsByUserId(userId);}


    /**
     * 根绝条件分页查看角色信息
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();//页码
        Integer pageSize = queryPageBean.getPageSize();//页面显示信息条数
        String queryString = queryPageBean.getQueryString();//搜索关键字

        PageHelper.startPage(currentPage, pageSize);
        List<Role> list = roleDao.findPage(queryString);
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        List<Role> roleList = pageInfo.getList();//该页面显示的信息
        long total = pageInfo.getTotal();//获取的信息的总条数
        PageResult pageResult = new PageResult(total, roleList);
        return pageResult;
    }

    /**
     * 查看全部的权限信息和菜单项信息
     *
     * @return
     */
    @Override
    public Map findPermissionAndMenuAll() {
        HashMap<Object, Object> hashMap = new HashMap<>();

        List<Permission> permissionList = permissionDao.selectAll();
        List<Menu> fathermenuList = menuDao.selectFatherMenuAll();
        List<Menu> menuList = menuDao.selectSubMenuAll();

        hashMap.put("permissionList", permissionList);
        hashMap.put("menuList", menuList);
        hashMap.put("fathermenuList", fathermenuList);

        return hashMap;
    }

    /**
     * 添加角色  添加角色和菜单的关联关系  添加权限的关联关系
     *
     * @param role
     * @param menuIds
     * @param permissionIds
     */
    @Override
    public void add(Role role, Integer[] menuIds, Integer[] permissionIds) {

        roleDao.add(role);
        Integer roleId = role.getId();

        //添加角色和菜单的关联关系
        this.setRoleAndMenuRelation(menuIds, roleId);
        //添加角色和权限的关联关系
        this.setRoleAndPermissionRelation(permissionIds, roleId);
    }

    /**
     * 根据id删除角色信息
     *
     * @param id
     */
    @Override
    public Result delete(Integer id) {
        //首先判断有没有用户拥有该角色信息有可以删除  没有提示有用户正在使用该角色
        List listRoleIds = roleDao.findAllUserIdByRoleId(id);
        if (listRoleIds != null && listRoleIds.size() > 0) {
            return new Result(false, MessageConstant.DELETE_ROLE_USER_USE_FAIL);
        }
        //删除关联的权限信息
        this.deleteRoleAndPermissionRelation(id);
        //删除关联的菜单信息
        this.deleteRoleAndMenuRelation(id);
        //删除角色基本信息
        roleDao.delete(id);
        return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
    }

    /**
     * 查看角色关联的权限id和菜单id
     *
     * @param id
     * @return
     */
    @Override
    public Map findPermissionIdsAndMenuIdsByRoleID(Integer id) {

        List permissionIdset = roleDao.findPermissionIds(id);
        List menuIdset = roleDao.findMenuIdsByRoleID(id);

        HashMap<Object, Object> hashMap = new HashMap<>();
        if (permissionIdset != null) {
            hashMap.put("PermissionIds", permissionIdset);
        }
        if (menuIdset != null) {
            hashMap.put("MenuIds", menuIdset);
        }
        return hashMap;
    }

    @Override
    public Role findById(Integer id) {

        Role role=roleDao.findById(id);
        return role;
    }

    @Override
    public void update(Role role, Integer[] menuIds, Integer[] permissionIds) {
        if ( role != null) {
            Integer roleId = role.getId();
            if (menuIds != null) {
                //删除关联的菜单信息
                this.deleteRoleAndMenuRelation(roleId);
                this.setRoleAndMenuRelation(menuIds, roleId);
            }
            if (menuIds != null) {
                //删除关联的权限信息
                this.deleteRoleAndPermissionRelation(roleId);
                //添加关联关系
                this.setRoleAndPermissionRelation(permissionIds,roleId);
            }

            //修改角色基本信息

            roleDao.update(role);

        }
    }

    //添加角色和权限的关联关系
    private void setRoleAndPermissionRelation(Integer[] permissionIds, Integer roleId) {
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                HashMap<Object, Object> hashMap = new HashMap<>();
                hashMap.put("roleId", roleId);
                hashMap.put("permissionId", permissionId);
                roleDao.setRoleAndPermissionRelation(hashMap);
            }
        }
    }

    //添加角色和菜单的关联关系
    private void setRoleAndMenuRelation(Integer[] menuIds, Integer roleId) {
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                HashMap<Object, Object> hashMap = new HashMap<>();
                hashMap.put("roleId", roleId);
                hashMap.put("menuId", menuId);
                roleDao.setRoleAndMenuRelation(hashMap);
            }
        }
    }

    //删除角色和权限的关联关系
    private void deleteRoleAndPermissionRelation(Integer roleId) {
        roleDao.deleteRoleAndPermissionRelation(roleId);
    }

    //删除角色和菜单的关联关系
    private void deleteRoleAndMenuRelation(Integer roleId) {
        roleDao.deleteRoleAndMenuRelation(roleId);
    }
}
